const fs = require('fs');
const path = require('path');
const parser = require('@babel/parser');
const traverse = require('@babel/traverse').default;  // 遍历
const babel = require('@babel/core');

function stepOne(filename) {
    // 同步式读取
    const content = fs.readFileSync(filename, 'utf-8');
    // console.log(content);
    // 选择合适的数据结构，  AST  抽象语法树
    const ast = parser.parse(content, {
        sourceType: 'module'
    })
    // console.log(ast);
    // 处理依赖关系
    const dependencies = {};
    traverse(ast, {
        ImportDeclaration({node}) {
            console.log(node);
            const dirname = path.dirname(filename);  // 目录
            // console.log(dirname);  // ./src
            const newFile = './' + path.join(dirname, node.source.value);
            // 依赖收集器
            dependencies[node.source.value] = newFile;
        }
    })
    // console.log(dependencies);
    const { code } = babel.transformFromAst(ast, null, {
        presets: ["@babel/preset-env"]
    })
    // var _message = _interopRequireDefault(require("./message.js"));  import 代码
    console.log(code);
}

stepOne('./src/index.js');